<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
        body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微软雅黑";}
    </style>
    <script type="text/javascript" src="//api.map.baidu.com/api?v=2.0&ak=pjVqtTnpjWWLXWYPksU8gb3eabQngWqN"></script>
    <title>GPS转百度</title>
</head>
<body>
    <div id="allmap" style="width: 500px;height: 500px;"></div>
    <div id="r-result">
		<input type="button" onclick="add_control();" value="添加" />
		<input type="button" onclick="delete_control();" value="删除" />
	</div>
</body>
</html>
<script type="text/javascript">
	
	var map = new BMap.Map("allmap");
		var point = new BMap.Point(116.331398,39.897445);
		map.centerAndZoom(point,12);
	var top_left_control = new BMap.ScaleControl({anchor: BMAP_ANCHOR_TOP_LEFT});// 左上角，添加比例尺
		var top_left_navigation = new BMap.NavigationControl();  //左上角，添加默认缩放平移控件
		var top_right_navigation = new BMap.NavigationControl({anchor: BMAP_ANCHOR_TOP_RIGHT, type: BMAP_NAVIGATION_CONTROL_SMALL}); //右上角，仅包含平移和缩放按钮
	
	
	function getPosition(callback) {
		
		
		
		
		var geolocation = new BMap.Geolocation();
		geolocation.getCurrentPosition(function(r){
			if(this.getStatus() == BMAP_STATUS_SUCCESS){
				var mk = new BMap.Marker(r.point);
				map.addOverlay(mk);
				map.panTo(r.point);
//				callback(r.point.lng,r.point.lat);
//				alert('您的位置：'+r.point.lng+','+r.point.lat);
			}
			else {
//				alert('failed'+this.getStatus());
			}        
		});
	}
	
	
	function map(x,y){
		console.log(x,y);
		//GPS坐标
		// 百度地图API功能
	    var ggPoint = new BMap.Point(x,y);
	
	    //地图初始化
	    var bm = new BMap.Map("allmap");
	    bm.centerAndZoom(ggPoint, 15);
	    bm.addControl(new BMap.NavigationControl());
	
	    //添加gps marker和label
	    var markergg = new BMap.Marker(ggPoint);
	    bm.addOverlay(markergg); //添加GPS marker
	    var labelgg = new BMap.Label("未转换的GPS坐标（错误）",{offset:new BMap.Size(20,-10)});
	    markergg.setLabel(labelgg); //添加GPS label
	
	    //坐标转换完之后的回调函数
	    translateCallback = function (data){
	      if(data.status === 0) {
	        var marker = new BMap.Marker(data.points[0]);
	        bm.addOverlay(marker);
	        var label = new BMap.Label("转换后的百度坐标（正确）",{offset:new BMap.Size(20,-10)});
	        marker.setLabel(label); //添加百度label
	        bm.setCenter(data.points[0]);
	      }
	    }
	
	    setTimeout(function(){
	        var convertor = new BMap.Convertor();
	        var pointArr = [];
	        pointArr.push(ggPoint);
	        convertor.translate(pointArr, 1, 5, translateCallback)
	    }, 1000);
	}
    
    
    	getPosition(map);
    	
    	
    	//添加控件和比例尺
	function add_control(){
		map.addControl(top_left_control);        
		map.addControl(top_left_navigation);     
		map.addControl(top_right_navigation);    
	}
	//移除控件和比例尺
	function delete_control(){
		map.removeControl(top_left_control);     
		map.removeControl(top_left_navigation);  
		map.removeControl(top_right_navigation); 
	}
</script>
